package my.app.taxi;

import my.app.R;
import my.app.ServiceClient;
import my.app.company.Company;
import my.app.company.CompanyDialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TaxiDialog extends Dialog{

	Taxi taxi = null;
	String phoneNumber = null;
	
	public TaxiDialog(Context context, final Taxi taxi) {
		
		
		super(context);
		
		
		this.taxi = taxi;

		setContentView(R.layout.taxi_dialog);
		
		
		
		
		
		setTitle(" :: Taxi Information ::.");
		
		//Set Cab Icon
		ImageView iv = (ImageView) findViewById(R.id.taxi_dialog_introduction_image);
		iv.setImageResource(ServiceClient.getDrawableId(taxi.getIcon().replace("t", "c")));
		
		//Set com name..
		TextView plate = (TextView) findViewById(R.id.taxi_dialog_introduction_companyname);
		plate.setText(taxi.getComp_name());
		
		//Set Telephone.
		TextView phone = (TextView) findViewById(R.id.taxi_dialog_phone_text);
		phone.setText(taxi.getTel());
		phoneNumber = taxi.getTel().substring(1);
		
		//Set Rating Stars..
		ImageView stars = (ImageView) findViewById(R.id.taxi_dialog_rating_1);
		stars.setImageResource(ServiceClient.getStarID(taxi.getRating().toString()));
		
		//Set Rating Count...
		TextView rat = (TextView) findViewById(R.id.taxi_dialog_rating_text);
		rat.setText("( "+taxi.getRatingCount().toString()+" ratings )");
		
		//Set Services..
		TextView servs = (TextView) findViewById(R.id.taxi_dialog_services);
		String s = "";
		for (String ss : taxi.getServices()) {
			
			s = s+ss+", ";
		}
		s = s.substring(0, s.length()-2);
		s = s.toUpperCase();
		servs.setText(s);
		
		//Set Licance Plate.
		TextView licence = (TextView) findViewById(R.id.taxi_dialog_inf_licanceplate);
		licence.setText("Licance Plate:  "+taxi.getLicenceTag());
		
		//Set car model.
		TextView car = (TextView) findViewById(R.id.taxi_dialog_inf_carmodel);
		car.setText("Car Model:  "+taxi.getCarModel());
		
		//Set Distace..
		TextView distance = (TextView) findViewById(R.id.taxi_dialog_inf_distance);
		distance.setText("Distace:  "+taxi.getDistance().toString()+ " KM   (bird's eye)");
		
		//Set Comments
		TextView commnet = (TextView) findViewById(R.id.taxi_dialog_inf_company);
		commnet.setText("About:  "+taxi.getInformation());
		
		
		
		
		
		
		
		//Listeners...
		//Listener Ratings.
		LinearLayout lrating = (LinearLayout) findViewById(R.id.taxi_dialog_rating);
		lrating.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TaxiRating t = new TaxiRating(getContext());
				t.show();
				
			}
		});
		
		//Listener OK Button..
		Button b = (Button) findViewById(R.id.taxi_dialog_okbutton);
		b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	dismiss();
            }
        });
		
		//Listener Phone..
		LinearLayout lphone = (LinearLayout) findViewById(R.id.taxi_dialog_phone);
		lphone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String uri = "tel:"+phoneNumber;
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse(uri));
				getContext().startActivity(intent);
				
			}
		});
		
		//Listener send message button..
		Button lsendbutton = (Button) findViewById(R.id.taxi_dialog_sendbutton);
		lsendbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SmsManager sms = SmsManager.getDefault();
				sms.sendTextMessage(phoneNumber, null, "PassInf: 40,20", null, null);
				
				
				
				AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
				builder.setMessage("Message Sent.")
				       .setCancelable(false)
				       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
		
		//Listener Company Dialog.
		plate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Company c = ServiceClient.getInstance(getContext()).getCompany(taxi.getComp_id());
				CompanyDialog cc = new CompanyDialog(getContext(), c);
				cc.show();
			}
		});
		
		
		
		
		//Dialogs View Settings.
		Window w = getWindow();
		WindowManager.LayoutParams lp = w.getAttributes();
		lp.width = WindowManager.LayoutParams.FILL_PARENT;
		lp.height = WindowManager.LayoutParams.FILL_PARENT;
		w.setAttributes(lp);


		
		

	}
	
	
    
	
	
	
	


	
	

	

}
